package com.dayaokeji.rhythmschoolstudent.client.common.exam;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter.ExamListViewPagerAdapter;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter.UnTestedExamListAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.ExamUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.ExamApi;
import com.dayaokeji.server_api.domain.Exam;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * 题库
 */
public class ExamActivity extends BaseActivity {

    public static final String EXTRA_REL_ID = "rel_id";
    public static final String EXTRA_REL_TYPE = "rel_type";
    public static final String EXTRA_IS_QUESTION_AND_ANSWER = "is_question_and_answer";

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tb_exam)
    TabLayout tbExam;
    @BindView(R.id.vp_exam)
    ViewPager vpExam;


    private String relId;
    private int relType;

    private static final ExamApi examApi = ApiUtils.getApi(ExamApi.class);
    private Call<ServerResponse<List<Exam>>> listCall;

    private final ArrayList<Exam> unTestedExam = new ArrayList<>();
    private final ArrayList<Exam> testedExam = new ArrayList<>();
    private ExamListViewPagerAdapter examListViewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exam);
        setSupportActionBar(toolbar);

        relId = getIntent().getStringExtra(EXTRA_REL_ID);
        relType = getIntent().getIntExtra(EXTRA_REL_TYPE, -1);

        if (relType == -1) {
            ToastUtils.warning(R.string.data_incorrect);
            return;
        }

        init();
    }

    private void init() {
        setupView();
        requestExamList();
    }

    private void setupView() {
        examListViewPagerAdapter = new ExamListViewPagerAdapter(getSupportFragmentManager());
        vpExam.setAdapter(examListViewPagerAdapter);
        tbExam.setupWithViewPager(vpExam);
    }

    private void requestExamList() {
        listCall = examApi.list(relId, relType);
        listCall.enqueue(new ServerCallback<List<Exam>>(this,"加载中...") {
            @Override
            public void onResponse(boolean success, ServerResponse<List<Exam>> response) {
                if (success) {
                    List<Exam> exams = response.getBody();
                    for (Exam exam : exams) {
                        if (!exam.getName().contains("【拍照问答】")) {
                            if (exam.getStatus() == 3) {
                                unTestedExam.add(exam);
                            }else{
                                testedExam.add(exam);
                            }
                        }

                    }

                    setFragments();
                }
            }
        });
    }

    private void setFragments() {
        final ArrayList<Fragment> fragments = new ArrayList<>();
        TestFragment unTestedFragment = TestFragment.Companion.newInstance(unTestedExam, TestFragment.Companion.getTYPE_UNTESTED());
        TestFragment testedFragment = TestFragment.Companion.newInstance(testedExam, TestFragment.Companion.getTYPE_TESTED());

        fragments.add(unTestedFragment);
        fragments.add(testedFragment);

        examListViewPagerAdapter.setFragments(fragments);
    }

    @Override
    protected void onDestroy() {
        if (listCall != null) {
            listCall.cancel();
        }
        super.onDestroy();
    }
}
